<?PHP
/*   This is a BSD style permissive license.
*    This module is original work by the author.
*
* Copyright (c) 2007, iNetOffice, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of iNetOffice nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY iNetOffice ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL iNetOffice BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Author: Tom Snyder, iNetOffice, Inc.
*/


/* -- opensam_tryit.php --
 *
 * Visit the web pages served by this module to see the OpenSAM open
 * source helpers in action.
 *
 */
 
require_once( "opensam_sso.php" );
 
// we use php default session handling services:
session_start();

if( !isset( $_SESSION ) ) $_SESSION = array(); // to avoid warnings and hassles.

// The $g_login_state_message tells the user their current logged in state or reports errors.
// The defaul logged-in message is the current state of the logged in session var:
if( empty( $_SESSION['state'] ) || $_SESSION['state'] != "loggedin" ) {
	$g_login_state_message = "You are <font color=red><strong>Logged Out</strong></font> of the sample login and browsing UI below.";
} else {
	$g_login_state_message = "You are <font color=#00A000><strong>Logged In</strong> as '".$_SESSION['StorageUserName']."'</font> to the sample login and browsing UI below.";
}


// These are out params:
$g_StorageServerUrl = $g_StorageUserName = $g_StorageSessionId = $g_StoragePassword = $g_WebDAVUrlParameters = null;

// Attempt an SSO log in using the opensam SSO helper. The helper uses the SSO CGI parameters.
// The SSO CGI parameters might be present in a variety of requests, so we check for them every time.
$sso_ret = opensam_sso_authenticate( $g_StorageServerUrl, $g_StorageUserName, $g_StorageSessionId, $g_StoragePassword, 
  $g_StorageDomainToConfirm, $g_WebDAVUrlParameters, $g_HTTPStatus );

if( $sso_ret === 0 ) {
	; // NO SSO ATTEMPT MADE. No CGI parameters found.
} 
else if( $sso_ret === true ) {
	// Successful login, establish the session:
	$_SESSION['state'] = 'loggedin';
	$_SESSION['StorageServerUrl'] = $g_StorageServerUrl;
	$_SESSION['StorageUserName']  = $g_StorageUserName;
	$_SESSION['StorageSessionId'] = $g_StorageSessionId;  
	$_SESSION['StoragePassword']  = $g_StoragePassword;  
	if( !empty( $g_WebDAVUrlParameters ) ) $_SESSION['WebDAVUrlParameters'] = $g_WebDAVUrlParameters;
	else unset( $_SESSION['WebDAVUrlParameters'] ); // no leftovers please.
	
	$g_login_state_message = "Log in succeeded (HTTP status $g_HTTPStatus). Storage Domain and path to confirm for account is '$g_StorageDomainToConfirm'<br>";
	$g_login_state_message .= "You are <font color=#00A000><strong>Logged In</strong></font> as '$g_StorageUserName'</font> to the sample login and browsing UI below.";
}
else {
	// SSO login attempt failed:
	$_SESSION['state'] = 'loggedout';
	unset( $_SESSION['StorageServerUrl'] );
	unset( $_SESSION['StorageSessionId'] );
	unset( $_SESSION['StoragePassword'] );
	unset( $_SESSION['StorageUserName'] );
	unset( $_SESSION['WebDAVUrlParameters'] );
	$g_login_state_message = "<font style=\"background-color:yellow;\">".$sso_ret."</font>";
}


print_page( $g_login_state_message );



function print_page( $login_state_message ) {
  global $g_StorageServerUrl;
  global $g_StorageUserName;
  global $g_StorageSessionId;
  global $g_StoragePassword;

  // Inherit from CGI if needed:  
  if( empty( $g_StorageServerUrl ) && array_key_exists( 'StorageServerUrl', $_GET ) ) $g_StorageServerUrl= $_GET['StorageServerUrl'];
  if( empty( $g_StorageUserName )  && array_key_exists( 'StorageUserName', $_GET ) )  $g_StorageUserName = $_GET['StorageUserName'];
  if( empty( $g_StorageSessionId ) && array_key_exists( 'StorageSessionId', $_GET ) ) $g_StorageSessionId= $_GET['StorageSessionId'];
  if( empty( $g_StoragePassword )  && array_key_exists( 'StoragePassword', $_GET ) )  $g_StoragePassword = $_GET['StoragePassword'];
?>
<HTML><HEAD><TITLE>OpenSAM Source Modules</TITLE>
<STYLE title=inetWord type=text/css>TD.iNetwordTDBrdr {
	BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.5pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.5pt; VERTICAL-ALIGN: top; BORDER-LEFT: black 1pt solid; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; netwrd-type: internal
}
TD.iNetwordTD {
	PADDING-RIGHT: 5.5pt; PADDING-LEFT: 5.5pt; VERTICAL-ALIGN: top; BACKGROUND-COLOR: transparent; netwrd-type: internal
}
TABLE.iNetwordTABLE {
	BORDER-COLLAPSE: collapse; netwrd-type: internal
}
H1.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 16pt; MARGIN: 12pt auto 3pt; FONT-FAMILY: Arial; mso-style-name: "Heading 1"
}
H2.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 14pt; MARGIN: 12pt auto 3pt; FONT-STYLE: italic; FONT-FAMILY: Arial; mso-style-name: "Heading 2"
}
H3.MsoNormal {
	FONT-WEIGHT: bold; FONT-SIZE: 13pt; MARGIN: 12pt auto 3pt; FONT-FAMILY: Arial; color: white; mso-style-name: "Heading 3"
}
.iNetwordPageBreak {
	DISPLAY: block; BACKGROUND-IMAGE: url(/s/i/bg_pagebreak.gif); PAGE-BREAK-BEFORE: always; WIDTH: 100%; HEIGHT: 10px; netwrd-type: internal
}
TD {
	netwrd-type: internal
}
.MsoNormal {
	MARGIN-TOP: 0in; FONT-WEIGHT: 400; FONT-SIZE: 11pt; MARGIN-BOTTOM: 0in; TEXT-INDENT: 0pt; FONT-STYLE: normal; FONT-FAMILY: Tahoma; TEXT-ALIGN: left; mso-style-name: "Normal"
}
</STYLE>
<SCRIPT type="text/javascript">
function update_url() {
  var anchor_el = document.getElementById( "LaunchLinkID" );
  var input_ServerUrl = document.getElementById( "StorageServerUrlID" );
  var input_UserName  = document.getElementById( "StorageUserNameID" );
  var input_SessionId  = document.getElementById( "StorageSessionId" );
  var input_Password  = document.getElementById( "StoragePassword" );
  anchor_el.href = "opensam_tryit.php?StorageServerUrl=" + encodeURIComponent(input_ServerUrl.value)
                    + "&StorageUserName=" + encodeURIComponent(input_UserName.value)
                    + "&StorageSessionId=" + encodeURIComponent(input_SessionId.value)
                    + "&StoragePassword=" + encodeURIComponent(input_Password.value);
  anchor_el.innerText = "opensam_tryit.php?StorageServerUrl=" + encodeURIComponent(input_ServerUrl.value)
                    + "\n&StorageUserName=" + encodeURIComponent(input_UserName.value)
                    + "\n&StorageSessionId=" + encodeURIComponent(input_SessionId.value)
                    + "\n&StoragePassword=" + encodeURIComponent(input_Password.value);
}
function ourserver() {
  var input_ServerUrl = document.getElementById( "StorageServerUrlID" );
  var input_UserName  = document.getElementById( "StorageUserNameID" );
  var input_Password  = document.getElementById( "StoragePassword" );
  input_ServerUrl.value = "http://webdav.inetapps.com";
  input_UserName.value = "johndoe";
  input_Password.value = "donttell";
  update_url();
}

function FileClickedCallback( context, filename ) {
	alert( "File browser called our FileClickedCallback() routine with filename: "+filename+" (context: "+context+")" );
}
</SCRIPT>
</HEAD>
<BODY id=x_undo1000 style="MARGIN: 14px" Ccomments="0" ccomments="0" wdwebmargins="14 14 14 14" wdpagemargins=".25 .25 .25 .25">
<H1 class=MsoNormal id=x_undo1001><A href="http://www.opensam.org">OpenSAM</A> Open Source Modules</H1>
<P class=MsoNormal><FONT style="FONT-SIZE: 11pt; FONT-FAMILY: Tahoma" face=Tahoma size=3>
<?PHP print $login_state_message; ?>
</P>
<P class=MsoNormal id=x_undo1003>&nbsp;</P>
<P class=MsoNormal id=x_undo1004>&nbsp;</P>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>
<TABLE class=iNetwordTABLE style="MARGIN-LEFT: 0px; MARGIN-RIGHT: auto; BORDER-COLLAPSE: collapse; BACKGROUND-IMAGE: url(i/tablebody_bg.jpg)" cellSpacing=0 cellPadding=0 border=0>
<TBODY>
<TR>
<TD class=iNetwordTD id=x_undo1005 style="BACKGROUND-IMAGE: url(i/tabletitle_bg.gif); BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>
<H3 class=MsoNormal style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">Single Sign On</H3></DIV></TD>
<TD class=iNetwordTD style="BACKGROUND-IMAGE: url(i/tabletitle_bg.gif); BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<H3 class=MsoNormal style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">Browse Files</H3></TD>
<TD class=iNetwordTD style="BACKGROUND-IMAGE: url(i/tabletitle_bg.gif); BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<H3 class=MsoNormal style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">
<H3 class=MsoNormal style="MARGIN-TOP: 0pt; MARGIN-BOTTOM: 0pt">Full WebDAV Client</H3></H3></TD></TR>
<TR>
<TD class=iNetwordTD id=x_undo1005 style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left></DIV>
<DIV class=MsoNormal id=x_undo1006 style="TEXT-ALIGN: left" align=left></DIV>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left></DIV>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left><A HREF="opensam_sso.php.txt">opensam_sso.php</A>, <A HREF="opensam_tryit.php.txt">opensam_tryit.php (this page)</A></DIV></TD>
<TD class=iNetwordTD style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<P class=MsoNormal><A HREF="opensam_browse_webdav.tar">opensam_browse_webdav.tar</A></P></TD>
<TD class=iNetwordTD style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<P class=MsoNormal><A HREF="opensam_webdav.tar">opensam_webdav.tar</A></P></TD></TR>
<TR>
<TD class=iNetwordTD id=x_undo1005 style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal id=x_undo1007 style="TEXT-ALIGN: left" align=left>Offers remote login using any WebDAV server.<br>Enter any WebDAV server below or 
<A href="javascript:ourserver()">try with our server</A></DIV>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>&nbsp;</DIV>
<P class=MsoNormal >StorageServerUrl:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageServerUrl id=StorageServerUrlID style="width:3in" onkeyup="update_url()"
<?PHP if( !empty( $g_StorageServerUrl ) ) print "value=\"$g_StorageServerUrl\""; ?>
></P>
<P class=MsoNormal >StorageUserName:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageUserName id=StorageUserNameID style="width:3in" onkeyup="update_url()"
<?PHP if( !empty( $g_StorageUserName ) ) print "value=\"$g_StorageUserName\""; ?>
></P>
<P class=MsoNormal >StorageSessionId:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StorageSessionId id=StorageSessionIdID style="width:3in" onkeyup="update_url()"
<?PHP if( !empty( $g_StorageSessionId ) ) print "value=\"$g_StorageSessionId\""; ?>
></P>
<P class=MsoNormal >or, StoragePassword:&nbsp;</P>
<P class=MsoNormal><INPUT type=text name=StoragePassword id=StoragePasswordID style="width:3in" onkeyup="update_url()"
<?PHP if( !empty( $g_StoragePassword ) ) print "value=\"$g_StoragePassword\""; ?>
></P>
<P class=MsoNormal>&nbsp;</P>
<P class=MsoNormal >SSO Launch Link:</P>
<P class=MsoNormal style="margin-left:.25in"><A id=LaunchLinkID href="/" target=_blank></A></P>
</TD>
<TD class=iNetwordTD style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<P class=MsoNormal>Offers this remote WebDAV folder browsing dialog box component in an IFRAME.</P>
<P class=MsoNormal>&nbsp;</P>
<DIV align=center><IFRAME src="opensam_browse_webdav.php?Context=demo" style="width:240px; height:300px"></IFRAME></DIV></TD>
<TD class=iNetwordTD style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<P class=MsoNormal>Offers full browse/read/write access to an OpenSAM WebDAV server.&nbsp;</P>
</TD></TR>
<TR>
<TD class=iNetwordTD id=x_undo1005 style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>Requirements:</DIV>
<UL style="LIST-STYLE: disc none outside; MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in" x_listseq="bd,1 ">
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>PHP 5.12 or greater.</DIV></LI>
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>modules: curl, session</DIV></LI>
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>This module is stand-alone and requires no other OpenSAM modules.</DIV></LI></UL>
<DIV class=MsoNormal id=x_undo1006 style="TEXT-ALIGN: left" align=left></DIV>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left></DIV>
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left></DIV></TD>
<TD class=iNetwordTD id=x_undo1008 style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>Requirements:</DIV>
<UL style="LIST-STYLE: disc none outside; MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in" x_listseq="bd,1 ">
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>PHP 5.12 or greater.</DIV></LI>
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>modules: curl, session</DIV></LI>
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>The OpenSAM Full WebDAV client code.</DIV></LI></UL>
<P class=MsoNormal>&nbsp;</P></TD>
<TD class=iNetwordTD id=x_undo1009 style="BORDER-RIGHT: #000000 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: #000000 1pt solid; PADDING-LEFT: 4pt; PADDING-BOTTOM: 4pt; BORDER-LEFT: #000000 1pt solid; WIDTH: 33.33%; PADDING-TOP: 4pt; BORDER-BOTTOM: #000000 1pt solid">
<DIV class=MsoNormal style="TEXT-ALIGN: left" align=left>Requirements:</DIV>
<UL style="LIST-STYLE: disc none outside; MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in" x_listseq="bd,1 ">
<LI style="FONT-SIZE: 11pt">
<DIV class=MsoNormal style="FONT-SIZE: 11pt; TEXT-ALIGN: left" align=left>PHP 5.12 or greater.</DIV></LI>
</UL>
<P class=MsoNormal>&nbsp;</P></TD></TR></TBODY></TABLE></DIV></BODY></HTML>

<?PHP
}   // end of function.
?>
